//
//  AcitivityTvc.m
//  MJApp
//
//  Created by John on 15/5/5.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "AcitivityTvc.h"
#import "ActivityCell.h"
#import "AcitivtyCellHeadView.h"
#import "MJRefresh.h"
#import "ActivityDetailVc.h"
#import "JoinUserViewController.h"

@interface AcitivityTvc ()<UITableViewDataSource,UITableViewDelegate,ActivityCellDelegate>

{

    NSInteger _page;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic, strong) NSMutableArray * actArr;
@property (nonatomic, strong) NSMutableArray * newActivityArr;
@property (nonatomic, strong) NSMutableArray * pastActivityArr;

@end

@implementation AcitivityTvc
//- (void)viewWillAppear:(BOOL)animated {
//    
//    [((TabBarController *)self.tabBarController) setTabbarHidden:NO];
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"将行";
//     self.tabBarController.tabBar.hidden = NO;
    self.hidesBottomBarWhenPushed = YES;
//    self.tableView.style = UITableViewStyleGrouped;
    NSLog(@"%ld",self.tableView.style);


    _page = 1;
    [self getAcitityList:NO];
    [self.tableView addHeaderWithTarget:self action:@selector(refrenshActivity)];
    [self.tableView addFooterWithTarget:self action:@selector(loadMoreActivity)];
//    [self.actArr addObject:self.newActivityArr];
//    [self.actArr addObject:self.pastActivityArr];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)refrenshActivity{
    [self getAcitityList:NO];
}
- (void)loadMoreActivity{

    [self getAcitityList:YES];
}
- (NSMutableArray *)actArr{

    if (_actArr == nil) {
        _actArr = [NSMutableArray array];
    }
    return _actArr;
}
- (NSMutableArray *)newActivityArr{
    
    if (_newActivityArr == nil) {
        _newActivityArr = [NSMutableArray array];
    }
    return _newActivityArr;
}
- (NSMutableArray *)pastActivityArr{
    
    if (_pastActivityArr == nil) {
        _pastActivityArr = [NSMutableArray array];
    }
    return _pastActivityArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)getAcitityList:(BOOL)isLoadMore{
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    if (isLoadMore) {
        ++_page;
    }else{
    
        _page = 1;
    }
    param[@"page"] = @(_page);
    


    
    [WLHTTPRequest getActivityListWithParam:param usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        [self.tableView headerEndRefreshing];
        [self.tableView footerEndRefreshing];
        NSArray * activityArr1 = resultDictionary[@"data"][@"new"];
        NSArray * activityArr2 = resultDictionary[@"data"][@"past"];
        
        if (!isLoadMore) {
            [self.actArr removeAllObjects];
            [self.newActivityArr removeAllObjects];
            [self.pastActivityArr removeAllObjects];
        }
        if (activityArr2.count==0&&activityArr1.count==0&&isLoadMore) {
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"没有更多数据"];
        }else if (activityArr1.count==0&&activityArr2.count==0&&!isLoadMore) {
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"暂时没有数据"];
        }else{
        
            
        }
        [self.newActivityArr addObjectsFromArray:activityArr1];
        [self.pastActivityArr addObjectsFromArray:activityArr2];
        [self.actArr removeAllObjects];
        [self.actArr addObject:self.newActivityArr];
        [self.actArr addObject:self.pastActivityArr];
        [self.tableView reloadData];
       
    } andFailureBlock:^(NSError *resultError) {
        [self.tableView headerEndRefreshing];
        [self.tableView footerEndRefreshing];
    }];

    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return self.actArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    NSArray * arr = self.actArr[section];
    return arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityCell * cell = [ActivityCell cellForTableView:tableView indexPath:indexPath];
    cell.infoDic = self.actArr[indexPath.section][indexPath.row];
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    ActivityCell * cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellH;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 50;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    AcitivtyCellHeadView * headView = [[NSBundle mainBundle] loadNibNamed:@"AcitivtyCellHeadView" owner:nil options:nil].lastObject;
    if(section == 0){
    
        headView.titleChinese.text = @"最新活动";
        headView.tilteEnglish.text = @"THE LATEST ACTIVITY";
    }else{
    
        headView.titleChinese.text = @"往期回顾";
        headView.tilteEnglish.text = @"PAST EVENTS";
    }
    return headView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    ActivityCell * cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    
    ActivityDetailVc * detailVc = [[ActivityDetailVc alloc] initWithDic:cell.infoDic];
    
    [self.navigationController pushViewController:detailVc animated:YES];
}


- (void)joinUser:(NSDictionary *)infoDic {
    
    [self.navigationController pushViewController:[[JoinUserViewController alloc] initWithID:infoDic[@"id"]] animated:YES];
}

- (void)comment:(NSDictionary *)infoDic {
    
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
